package cn.tedu.store.controller;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ExceptionHandler;

import cn.tedu.store.controller.ex.FileEmptyException;
import cn.tedu.store.controller.ex.FileSizeException;
import cn.tedu.store.controller.ex.FileTypeException;
import cn.tedu.store.controller.ex.FileUploadException;
import cn.tedu.store.controller.ex.FileUploadIoException;
import cn.tedu.store.controller.ex.FileUploadStateException;
import cn.tedu.store.service.ex.AccessDeniedException;
import cn.tedu.store.service.ex.AdderssNotFoundException;
import cn.tedu.store.service.ex.AddressCountLimitException;
import cn.tedu.store.service.ex.CartNotFoundException;
import cn.tedu.store.service.ex.InsertException;
import cn.tedu.store.service.ex.PasswordNotMatchException;
import cn.tedu.store.service.ex.ServiceException;
import cn.tedu.store.service.ex.UpdateException;
import cn.tedu.store.service.ex.UserNotFoundException;
import cn.tedu.store.service.ex.UsernameDuplicateException;
import cn.tedu.store.util.JsonResult;
/**
 * 控制器类基类，主要用于  统一处理异常
 * @author Administrator
 *
 */
public abstract class BaseController {
	//操作成功状态
	public static final Integer SUCCESS=2000;
	//仅子类会调用protected，且不会修改final
	/**
	 * 从session中获取uid
	 * @param session
	 * @return
	 */
	protected final Integer getUidFromSession(HttpSession session) {
		Integer uid=Integer.valueOf(session.getAttribute("uid").toString());
		return uid;
	}
	/**
	 * 从session中获取username
	 * @param session
	 * @return
	 */
	protected final String getUsernameFromSession(HttpSession session) {
		String username=session.getAttribute("username").toString();
		return username;
	}

	@ExceptionHandler({ ServiceException.class, FileUploadException.class })
	/**
	 * 以下方法由控制器自动调用，但之前必须有注解@ExceptionHandler(ServiceException.class)
	 * 
	 * @param e
	 * @return
	 */
	public JsonResult<Void> handlerException(Throwable e) {
		JsonResult<Void> jr = new JsonResult<Void>(SUCCESS);
		jr.setMessage(e.getMessage());
		if (e instanceof UsernameDuplicateException) {
			// 用户名冲突4000
			jr.setState(4000);
		} else if (e instanceof UserNotFoundException) {
			jr.setState(4001);
		} else if (e instanceof PasswordNotMatchException) {
			jr.setState(4002);
		} else if (e instanceof InsertException) {
			jr.setState(5000);
		} else if (e instanceof UpdateException) {
			jr.setState(5001);
		} else if (e instanceof FileEmptyException) {
			jr.setState(6000);
		} else if (e instanceof FileSizeException) {
			jr.setState(6001);
		} else if (e instanceof FileTypeException) {
			jr.setState(6002);
		} else if (e instanceof FileUploadIoException) {
			jr.setState(6003);
		} else if (e instanceof FileUploadStateException) {
			jr.setState(6004);
		}else if (e instanceof AddressCountLimitException) {
			jr.setState(7000);
		}else if (e instanceof AdderssNotFoundException) {
			jr.setState(7001);
		}else if (e instanceof AccessDeniedException) {
			jr.setState(7002);
		}else if (e instanceof CartNotFoundException) {
			jr.setState(7003);
		};

		return jr;
	}
	}

